package com.hhyx.execl.util;

import cn.hutool.core.util.StrUtil;
import com.hhyx.execl.handler.BusinessException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

/**
 * @author MarkerCCC
 * @date 2019/6/18 - 10:25
 * @email markerccc@163.com
 */
@Slf4j
public class ListUtils {
    /**
     * 查找集合中所有的重复元素,并进行显示
     *
     * @param list
     * @return
     */
    public static Map<String, Object> findAllRepeatedElements(
            List<Map<String, Object>> list) {
        HashMap<String, Object> map = new HashMap<>(16);
        HashSet<String> fileSet = new HashSet<>();
        HashSet<String> biduiSet = new HashSet<>(10);
        HashSet<String> fileSets = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            // 重复元素的下表列表
            ArrayList<String> subscriptList = new ArrayList<>(10);
            String idStr = list.get(i).get("下辖ID").toString();
            if (map.get(idStr) != null) {
                continue;
            }

            for (int j = i + 1; j < list.size(); j++) {
                // log.error("{}", list.get(j));
                // log.error("{}", list.get(j).get("下辖ID"));
                String idStrTwo;
                // try {
                    idStrTwo = list.get(j).get("下辖ID").toString();
                // } catch (Exception e) {
                //     throw new BusinessException("下辖ID不能为空, 当前文件为, 错误信息看上面的日志");
                // }
                if ((idStr.equals(idStrTwo)) && (i != j)) {

                    String s = "当前文件 :" + list.get(i).get("fileName") + "的 " + list.get(i).get("row")
                                    + " 行与文件 " + list
                                    .get(j).get("fileName") + " 的第" + list.get(j).get("row")
                                    + " 行重复";

                    subscriptList.add(s);

                    fileSet.add(list.get(i).get("fileName").toString());
                    fileSet.add(list.get(j).get("fileName").toString());

                    map.put(idStr, subscriptList);
                }
            }
        }
        map.put("file", fileSet);
        log.error("--------->");
        log.error("{}", map);
        log.error("--------->");
        return map;
    }

    public static void verifyRowIntegrity(List<Object> rowList) {
        String[] rowArr = {"序号", "所属事业部", "公司名称", "企业法人", "下辖ID归属人", "下辖ID", "电话",
                "指定收款ID（一个公司仅限一个）", "对公账户", "邮箱", "提交时间"};
        List<String> rowStrList = new ArrayList<>(Arrays.asList(rowArr));

        if (CollectionUtils.isEmpty(rowList)) {
            throw new BusinessException("表格不能为空");
        }

        String rowZeroStr = rowList.get(0).toString();
        String rowOneStr = rowList.get(1).toString();
        String rowTwoStr = rowList.get(2).toString();
        String rowThreeStr = rowList.get(3).toString();
        String rowFourStr = rowList.get(4).toString();
        String rowFiveStr = rowList.get(5).toString();
        String rowSixStr = rowList.get(6).toString();
        String rowSevenStr = rowList.get(7).toString();
        String rowEightStr = rowList.get(8).toString();
        String rowNineStr = rowList.get(9).toString();
        String rowTenStr = rowList.get(10).toString();
        if (StrUtil.isNotBlank(rowZeroStr)) {
            if (!rowStrList.get(0).equals(rowZeroStr)) {
                throw new BusinessException("该列不为序号列, 请修改");
            }
        } else {
            throw new BusinessException("序列号列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowOneStr)) {
            if (!rowStrList.get(1).equals(rowOneStr)) {
                throw new BusinessException("该列不为所属事业部列, 请修改");
            }
        } else {
            throw new BusinessException("所属事业部列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowTwoStr)) {
            if (!rowStrList.get(2).equals(rowTwoStr)) {
                throw new BusinessException("该列不为公司名称列, 请修改");
            }
        } else {
            throw new BusinessException("公司名称列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowThreeStr)) {
            if (!rowStrList.get(3).equals(rowThreeStr)) {
                throw new BusinessException("该列不为企业法人列, 请修改");
            }
        } else {
            throw new BusinessException("企业法人列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(4).equals(rowFourStr)) {
                throw new BusinessException("该列不为下辖ID归属人列, 请修改");
            }
        } else {
            throw new BusinessException("下辖ID归属人列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(5).equals(rowFiveStr)) {
                throw new BusinessException("该列不为下辖ID列, 请修改");
            }
        } else {
            throw new BusinessException("下辖ID列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(6).equals(rowSixStr)) {
                throw new BusinessException("该列不为电话列, 请修改");
            }
        } else {
            throw new BusinessException("电话列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(7).equals(rowSevenStr)) {
                throw new BusinessException("该列不为指定收款ID（一个公司仅限一个）列, 请修改");
            }
        } else {
            throw new BusinessException("指定收款ID（一个公司仅限一个）列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(8).equals(rowEightStr)) {
                throw new BusinessException("该列不为对公账户列, 请修改");
            }
        } else {
            throw new BusinessException("对公账户列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(9).equals(rowNineStr)) {
                throw new BusinessException("该列不为邮箱列, 请修改");
            }
        } else {
            throw new BusinessException("邮箱列, 表头不能为空");
        }

        if (StrUtil.isNotBlank(rowFourStr)) {
            if (!rowStrList.get(10).equals(rowTenStr)) {
                throw new BusinessException("该列不为提交时间列, 请修改");
            }
        } else {
            throw new BusinessException("提交时间列, 表头不能为空");
        }
    }
}
